package com.hengxinguotong.zhihuichengjian.ui.environment.spray;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.EnvironmentSprayListAdapter;
import com.hengxinguotong.zhihuichengjian.bean.EnvironmentSpray;
import com.hengxinguotong.zhihuichengjian.bean.EnvironmentSprayListRes;
import com.hengxinguotong.zhihuichengjian.bean.SprayThreshold;
import com.hengxinguotong.zhihuichengjian.bean.SprayThresholdRes;
import com.hengxinguotong.zhihuichengjian.ui.BaseActivity;
import com.hengxinguotong.zhihuichengjian.utils.Constants;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXEditTextView;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.hengxinguotong.zhihuichengjian.widget.view.PullableRecyclerView;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.http.RequestParams;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

/**
 * @CopyRight 恒信国通信息科技
 * @Author WuLei
 * @Date 2019/2/24
 * <p>
 * 喷淋控制
 */

public class EnvironmentSprayListActivity extends BaseActivity implements View.OnClickListener, EnvironmentSprayListAdapter.OnClickListener,
        EnvironmentSprayListAdapter.OnThresholdListener, EnvironmentSprayListAdapter.OnOpenSprayListener,
        EnvironmentSprayListAdapter.OnHandleSprayListener, EnvironmentSprayListAdapter.OnAutoSprayListener{
    @BindView(R.id.title_tv)
    HXTextView titleTv;
    @BindView(R.id.refresh_list)
    PullToRefreshLayout refreshList;
    @BindView(R.id.empty_img)
    ImageView emptyImg;
    @BindView(R.id.select_rl)
    RelativeLayout selectRl;
    @BindView(R.id.all_select)
    CheckBox allSelect;
    @BindView(R.id.open)
    HXTextView open;
    @BindView(R.id.close)
    HXTextView close;
    @BindView(R.id.auto)
    HXTextView auto;
    @BindView(R.id.back_tv)
    HXTextView backTv;
    @BindView(R.id.select_tv)
    HXTextView selectTv;

    private static final int GET_SETTINS = 1001;

    private float pm25Alarm = 0,//	pm25阈值
            pm10Alarm = 0,//	pm10阈值
            temperatureAlarm = 0,//	温度阈值
            humidityAlarm = 0;//	湿度阈值

    private PullableRecyclerView dataRv;

    private List<EnvironmentSpray> environmentSprayList,//喷淋设备列表
            selectEnvironmentSprayList,//选中的喷淋设备列表
            selectAutoEnvironmentSprayList;//选中的自动喷淋设备列表

    private EnvironmentSprayListAdapter environmentSprayListAdapter;
    private int pageIndex = 1;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constants.UPDATE:
                    List<EnvironmentSpray> datas = (List<EnvironmentSpray>) msg.obj;
                    bindData(datas);
                    break;
                case GET_SETTINS:
                    EnvironmentSpray spray = (EnvironmentSpray) msg.obj;
                    List<EnvironmentSpray> autoDatas = new ArrayList<>();
                    autoDatas.add(spray);
                    initDialog(autoDatas);
                    break;
            }
        }
    };

    private void bindData(List<EnvironmentSpray> datas) {
        refreshList.refreshFinish(PullToRefreshLayout.SUCCEED);
        for (EnvironmentSpray environmentSpray:datas){
            for (EnvironmentSpray spray:selectEnvironmentSprayList){
                if(environmentSpray.equals(spray)){
                    environmentSpray.setChecked(spray.isChecked());
                }
            }
        }
        environmentSprayList = datas;
        showEmptyView(environmentSprayList.size() == 0);
        environmentSprayListAdapter.setDataList(environmentSprayList);
    }

    private void showEmptyView(boolean isEmpty) {
        if (isEmpty) {
            emptyImg.setVisibility(View.VISIBLE);
        } else {
            emptyImg.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_environment_spray_list);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initView() {
        titleTv.setText(R.string.spray_control);

        selectRl.setVisibility(View.GONE);
//        selectTv.setVisibility(View.GONE);

        refreshList.setPullUpEnable(false);
        refreshList.setOnPullListener(pullListener);
        dataRv = (PullableRecyclerView) refreshList.getPullableView();
        selectEnvironmentSprayList = new ArrayList<>();
        selectAutoEnvironmentSprayList = new ArrayList<>();
        environmentSprayList = new ArrayList<>();
        environmentSprayListAdapter = new EnvironmentSprayListAdapter(this, environmentSprayList);
        environmentSprayListAdapter.setCanSelected(false);
        environmentSprayListAdapter.setOnClickListener(this);
        environmentSprayListAdapter.setOnThresholdListener(this);
        environmentSprayListAdapter.setOnOpenSprayListener(this);
        environmentSprayListAdapter.setOnAutoSprayListener(this);
        environmentSprayListAdapter.setOnHandleSprayListener(this);
        dataRv.setAdapter(environmentSprayListAdapter);
        dataRv.setItemAnimator(new DefaultItemAnimator());
        dataRv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    }

    private PullToRefreshLayout.OnPullListener pullListener = new PullToRefreshLayout.OnPullListener() {
        @Override
        public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
            pageIndex = 1;
            getEnvironmentSprayList(null);
        }

        @Override
        public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
            pageIndex = environmentSprayList.size() / Utils.ONCECOUNT + 1;
            getEnvironmentSprayList(null);
        }
    };

    private void initData() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                getEnvironmentSprayList("加载中…");
            }
        }, 500);
    }

    /**
     * 喷淋列表
     * @param alert
     */
    public void getEnvironmentSprayList(String alert) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("constructionId", SPUtil.getString(this, "constructionid"));
//            jsonObject.put("pageNum", pageIndex + "");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestParams params = new RequestParams();
        params.addHeader("Content-type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Utils.requestData(alert, this, "/equipmentRegister/getSparyList/" + SPUtil.getString(this, "uuid"), params, new Utils.ListenerJson() {
            @Override
            public void onSuccess(String json) {
                if (!Utils.isEmpty(json)) {
                    Gson gson = new Gson();
                    EnvironmentSprayListRes res = gson.fromJson(json, EnvironmentSprayListRes.class);
                    if (res.getStatus() == 1) {
                        Message msg = handler.obtainMessage();
                        msg.what = Constants.UPDATE;
                        msg.obj = res.getValue();
                        msg.sendToTarget();
                    } else {
                        showToast(res.getMsg());
                        if (pageIndex == 1) {
                            refreshList.refreshFinish(PullToRefreshLayout.FAIL);
                        } else {
                            refreshList.loadmoreFinish(PullToRefreshLayout.FAIL);
                        }
                    }
                } else {
                    showToast("系统异常");
                    if (pageIndex == 1) {
                        refreshList.refreshFinish(PullToRefreshLayout.FAIL);
                    } else {
                        refreshList.loadmoreFinish(PullToRefreshLayout.FAIL);
                    }
                }
            }

            @Override
            public void onFailure(String json) {
                try {
                    JSONObject object = new JSONObject(json);
                    String msg = object.getString("msg");
                    showToast(msg);
                } catch (JSONException e) {
                    e.printStackTrace();
                    showToast("系统异常");
                }
                if (pageIndex == 1) {
                    refreshList.refreshFinish(PullToRefreshLayout.FAIL);
                } else {
                    refreshList.loadmoreFinish(PullToRefreshLayout.FAIL);
                }
            }
        });
    }

    /**
     * 获取喷淋阈值
     * @param datas
     */
    public void getAlarmSetting(final EnvironmentSpray datas) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("equipmentId", datas.getId());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestParams params = new RequestParams();
        params.addHeader("Content-type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Utils.requestData("请稍等…", this, "/environment/getAlarmSetting/" + SPUtil.getString(this, "uuid"), params, new Utils.ListenerJson() {
            @Override
            public void onSuccess(String json) {
                if (!Utils.isEmpty(json)) {
                    Gson gson = new Gson();
                    SprayThresholdRes res = gson.fromJson(json, SprayThresholdRes.class);
                    if (res.getStatus() == 1) {
                        SprayThreshold sprayThreshold = res.getValue();
                        pm10Alarm = sprayThreshold.getPm10Alarm();
                        pm25Alarm = sprayThreshold.getPm25Alarm();
                        temperatureAlarm = sprayThreshold.getTemperatureAlarm();
                        humidityAlarm = sprayThreshold.getHumidityAlarm();
                        Message msg = handler.obtainMessage();
                        msg.what = GET_SETTINS;
                        msg.obj = datas;
                        msg.sendToTarget();
                    } else {
                        showToast(res.getMsg());
                    }
                } else {
                    showToast("系统异常");
                }
            }

            @Override
            public void onFailure(String json) {
                try {
                    JSONObject object = new JSONObject(json);
                    String msg = object.getString("msg");
                    showToast(msg);
                } catch (JSONException e) {
                    e.printStackTrace();
                    showToast("系统异常");
                }
            }
        });
    }

    /**
     * 修改或新增阈值
     * @param datas
     */
    public void saveOrUpdateSetting(List<EnvironmentSpray> datas) {
        JSONObject jsonObject = new JSONObject();
        StringBuffer sb = new StringBuffer();
        for (EnvironmentSpray environmentSpray:datas){
            sb.append(environmentSpray.getId() + ",");
        }
        try {
            jsonObject.put("idStr", sb.toString());
            jsonObject.put("pm10Alarm", pm10Alarm);
            jsonObject.put("pm25Alarm", pm25Alarm);
            jsonObject.put("temperatureAlarm", temperatureAlarm);
            jsonObject.put("humidityAlarm", humidityAlarm);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestParams params = new RequestParams();
        params.addHeader("Content-type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Utils.requestData("请稍等…", this, "/environment/saveOrUpdateSetting/" + SPUtil.getString(this, "uuid"), params, new Utils.ListenerJson() {
            @Override
            public void onSuccess(String json) {
                if (!Utils.isEmpty(json)) {
                    try {
                        JSONObject object = new JSONObject(json);
                        String msg = object.getString("msg");
                        int status = object.getInt("status");
//                        getEnvironmentSprayList(null);
                        showToast(msg);
                    } catch (JSONException e) {
                        e.printStackTrace();
                        showToast("系统异常");
                    }
                } else {
                    showToast("系统异常");
                }
            }

            @Override
            public void onFailure(String json) {
                try {
                    JSONObject object = new JSONObject(json);
                    String msg = object.getString("msg");
                    showToast(msg);
                } catch (JSONException e) {
                    e.printStackTrace();
                    showToast("系统异常");
                }
            }
        });
    }

    /**
     * 喷淋开启自动或手动
     * @param sprayStatus
     * @param datas
     */
    public void updateSparyStatus(final int sprayStatus, final List<EnvironmentSpray> datas) {
        JSONObject jsonObject = new JSONObject();
        StringBuffer sb = new StringBuffer();
        for (EnvironmentSpray environmentSpray:datas){
            sb.append(environmentSpray.getId() + ",");
        }
        try {
            jsonObject.put("idStr", sb.toString());
            jsonObject.put("sprayStatus", sprayStatus);//喷淋状态:0手动,1自动
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestParams params = new RequestParams();
        params.addHeader("Content-type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Utils.requestData("请稍等…", this, "/equipmentRegister/updateSparyStatus/" + SPUtil.getString(this, "uuid"), params, new Utils.ListenerJson() {
            @Override
            public void onSuccess(String json) {
                if (!Utils.isEmpty(json)) {
                    try {
                        JSONObject object = new JSONObject(json);
                        String msg = object.getString("msg");
                        int status = object.getInt("status");
//                        getEnvironmentSprayList(null);
                        showToast(msg);
                    } catch (JSONException e) {
                        e.printStackTrace();
                        showToast("系统异常");
                    }
                } else {
                    showToast("系统异常");
                }
            }

            @Override
            public void onFailure(String json) {
                try {
                    JSONObject object = new JSONObject(json);
                    String msg = object.getString("msg");
                    showToast(msg);
                } catch (JSONException e) {
                    e.printStackTrace();
                    showToast("系统异常");
                }
            }

            private void dataSieves(boolean result){
                for (EnvironmentSpray environmentSpray:environmentSprayList){
                    for (EnvironmentSpray spray:datas){
                        if(environmentSpray.equals(spray)){
                            if(result){
                                if(sprayStatus == 1){
                                    environmentSpray.setSprayStatus(1);//喷淋状态:0手动,1自动
                                }else {
                                    environmentSpray.setSprayStatus(0);
                                }
                            }else {
                                if(sprayStatus == 1){
                                    environmentSpray.setSprayStatus(0);
                                }else {
                                    environmentSpray.setSprayStatus(1);
                                }
                            }
                        }
                    }
                }
                environmentSprayListAdapter.setDataList(environmentSprayList);
            }
        });
    }

    /**
     * 批处理开启或关闭喷淋
     * @param type
     * @param datas
     */
    public void batchSpray(final int type, final List<EnvironmentSpray> datas) {
        JSONObject jsonObject = new JSONObject();
        StringBuffer sb = new StringBuffer();
        for (EnvironmentSpray environmentSpray:datas){
            sb.append(environmentSpray.getId() + ",");
        }
        try {
            jsonObject.put("idStr", sb.toString());
            jsonObject.put("type", type);// type 1开 2关
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestParams params = new RequestParams();
        params.addHeader("Content-type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Utils.requestData("请稍等…", this, "/spray/batchSpray/" + SPUtil.getString(this, "uuid"), params, new Utils.ListenerJson() {
            @Override
            public void onSuccess(String json) {
                if (!Utils.isEmpty(json)) {
                    try {
                        JSONObject object = new JSONObject(json);
                        String msg = object.getString("msg");
                        int status = object.getInt("status");
                        String value = object.getString("value");
                        if(status == 1){
                            refreshDialog(value);
                        }else {
                            showToast(msg);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        showToast("系统异常");
                    }
                } else {
                    showToast("系统异常");
                }
            }

            @Override
            public void onFailure(String json) {
                try {
                    JSONObject object = new JSONObject(json);
                    String msg = object.getString("msg");
                    showToast(msg);
                } catch (JSONException e) {
                    e.printStackTrace();
                    showToast("系统异常");
                }
            }

//            private void dataSieves(boolean result){
//                for (EnvironmentSpray environmentSpray:environmentSprayList){
//                    for (EnvironmentSpray spray:datas){
//                        if(environmentSpray.equals(spray)){
//                            if(result){
//                                if(type == 1){
//                                    environmentSpray.setStatus("1111");
//                                }else {
//                                    environmentSpray.setStatus("0000");
//                                }
//                            }else {
//                                if(type == 2){
//                                    environmentSpray.setStatus("1111");
//                                }else {
//                                    environmentSpray.setStatus("0000");
//                                }
//                            }
//                        }
//                    }
//                }
//                environmentSprayListAdapter.setDataList(environmentSprayList);
//            }
        });
    }

    /**
     * 喷淋开启
     * @param datas
     */
    public void startSpary(final EnvironmentSpray datas) {
        Utils.requestData("请稍等…", this, "/spray/start/" + SPUtil.getString(this, "uuid") + "?equipmentId=" + datas.getId(), null, new Utils.ListenerJson() {
            @Override
            public void onSuccess(String json) {
                if (!Utils.isEmpty(json)) {
                    try {
                        JSONObject object = new JSONObject(json);
                        String msg = object.getString("msg");
                        int status = object.getInt("status");
                        if(status == 1){
                            dataSieves(true);
                        }else {
                            dataSieves(false);
                        }
                        showToast(msg);
                    } catch (JSONException e) {
                        e.printStackTrace();
                        dataSieves(false);
                        showToast("系统异常");
                    }
                } else {
                    dataSieves(false);
                    showToast("系统异常");
                }
            }

            @Override
            public void onFailure(String json) {
                dataSieves(false);
                try {
                    JSONObject object = new JSONObject(json);
                    String msg = object.getString("msg");
                    showToast(msg);
                } catch (JSONException e) {
                    e.printStackTrace();
                    showToast("系统异常");
                }
            }

            private void dataSieves(boolean result){
                for (EnvironmentSpray environmentSpray:environmentSprayList){
                    if(environmentSpray.equals(datas)){
                        if(result){
                            environmentSpray.setStatus("1111");
                        }else {
                            environmentSpray.setStatus("0000");
                        }
                    }
                }
                environmentSprayListAdapter.setDataList(environmentSprayList);
            }
        });
    }

    /**
     * 喷淋关闭
     * @param datas
     */
    public void stopSpary(final EnvironmentSpray datas) {
        Utils.requestData("请稍等…", this, "/spray/stop/" + SPUtil.getString(this, "uuid") + "?equipmentId=" + datas.getId(), null, new Utils.ListenerJson() {
            @Override
            public void onSuccess(String json) {
                if (!Utils.isEmpty(json)) {
                    try {
                        JSONObject object = new JSONObject(json);
                        String msg = object.getString("msg");
                        int status = object.getInt("status");
                        if(status == 1){
                            dataSieves(true);
                        }else {
                            dataSieves(false);
                        }
                        showToast(msg);
                    } catch (JSONException e) {
                        e.printStackTrace();
                        dataSieves(false);
                        showToast("系统异常");
                    }
                } else {
                    dataSieves(false);
                    showToast("系统异常");
                }
            }

            @Override
            public void onFailure(String json) {
                dataSieves(false);
                try {
                    JSONObject object = new JSONObject(json);
                    String msg = object.getString("msg");
                    showToast(msg);
                } catch (JSONException e) {
                    e.printStackTrace();
                    showToast("系统异常");
                }
            }

            private void dataSieves(boolean result){
                for (EnvironmentSpray environmentSpray:environmentSprayList){
                    if(environmentSpray.equals(datas)){
                        if(result){
                            environmentSpray.setStatus("0000");
                        }else {
                            environmentSpray.setStatus("1111");
                        }
                    }
                }
                environmentSprayListAdapter.setDataList(environmentSprayList);
            }
        });
    }

    @OnClick({R.id.back_tv, R.id.select_tv, R.id.all_select, R.id.open, R.id.close, R.id.auto, R.id.select_rl})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.select_rl:
                break;
            case R.id.back_tv:
                finish();
                break;
            case R.id.select_tv:
                if (selectRl.getVisibility() == View.VISIBLE) {
                    selectRl.setVisibility(View.GONE);
                    environmentSprayListAdapter.setCanSelected(false);
                } else {
                    selectRl.setVisibility(View.VISIBLE);
                    environmentSprayListAdapter.setCanSelected(true);
                }
                break;
            case R.id.all_select:
                if (allSelect.isChecked()) {
                    if (environmentSprayList.size() > 0) {
                        for (EnvironmentSpray environmentSpray : environmentSprayList) {
                            environmentSpray.setChecked(allSelect.isChecked());
                            if(!selectEnvironmentSprayList.contains(environmentSpray)){
                                selectEnvironmentSprayList.add(environmentSpray);
                                if (!selectAutoEnvironmentSprayList.contains(environmentSpray) && environmentSpray.getSprayStatus() == 1) {//喷淋状态:0手动,1自动
                                    selectAutoEnvironmentSprayList.add(environmentSpray);
                                }
                            }
                        }
                    }
                } else {
                    if (environmentSprayList.size() > 0) {
                        for (EnvironmentSpray environmentSpray : environmentSprayList) {
                            environmentSpray.setChecked(allSelect.isChecked());
                            if(selectEnvironmentSprayList.contains(environmentSpray)){
                                selectEnvironmentSprayList.remove(environmentSpray);
                                if (selectAutoEnvironmentSprayList.contains(environmentSpray)) {//喷淋状态:0手动,1自动
                                    selectAutoEnvironmentSprayList.remove(environmentSpray);
                                }
                            }
                        }
                    }
                }
                environmentSprayListAdapter.setDataList(environmentSprayList);
                if (selectEnvironmentSprayList.size() > 0) {
                    open.setEnabled(true);
                    open.setClickable(true);
                    close.setEnabled(true);
                    close.setClickable(true);
                    if (selectAutoEnvironmentSprayList.size() > 0) {
                        auto.setEnabled(true);
                        auto.setClickable(true);
                    } else {
                        auto.setEnabled(false);
                        auto.setClickable(false);
                    }
                } else {
                    open.setEnabled(false);
                    open.setClickable(false);
                    close.setEnabled(false);
                    close.setClickable(false);
                    auto.setEnabled(false);
                    auto.setClickable(false);
                }
                break;
            case R.id.open:
                if(selectEnvironmentSprayList.size() > 0){
                    batchSpray(1, selectEnvironmentSprayList);//1开 2关
                }
                break;
            case R.id.close:
                if(selectEnvironmentSprayList.size() > 0){
                    batchSpray(2, selectEnvironmentSprayList);//1开 2关
                }
                break;
            case R.id.auto:
                if(selectAutoEnvironmentSprayList.size() > 0){
                    initDialog(selectAutoEnvironmentSprayList);
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 0 && data != null) {
            }
        }
    }

    @Override
    public void onClick(Object o) {
        EnvironmentSpray environmentSpray = (EnvironmentSpray) o;
        if (environmentSpray == null) {
            return;
        }
        if(environmentSpray.isChecked()){
            if(selectEnvironmentSprayList.contains(environmentSpray)){
                selectEnvironmentSprayList.remove(environmentSpray);
                if(selectAutoEnvironmentSprayList.contains(environmentSpray)) {
                    selectAutoEnvironmentSprayList.remove(environmentSpray);
                }
            }
        }else {
            if(!selectEnvironmentSprayList.contains(environmentSpray)){
                selectEnvironmentSprayList.add(environmentSpray);
                if(!selectAutoEnvironmentSprayList.contains(environmentSpray) && (environmentSpray.getSprayStatus() == 1)) {//	喷淋状态:0手动,1自动
                    selectAutoEnvironmentSprayList.add(environmentSpray);
                }
            }
        }
        environmentSpray.setChecked(!environmentSpray.isChecked());
        environmentSprayListAdapter.setDataList(environmentSprayList);
        if(selectEnvironmentSprayList.size() > 0){
            open.setEnabled(true);
            open.setClickable(true);
            close.setEnabled(true);
            close.setClickable(true);
            if(selectAutoEnvironmentSprayList.size() > 0){
                auto.setEnabled(true);
                auto.setClickable(true);
            }else {
                auto.setEnabled(false);
                auto.setClickable(false);
            }
            if(selectEnvironmentSprayList.size() == environmentSprayList.size()){
                allSelect.setChecked(true);
            }else {
                allSelect.setChecked(false);
            }
        }else {
            allSelect.setChecked(false);
            open.setEnabled(false);
            open.setClickable(false);
            close.setEnabled(false);
            close.setClickable(false);
            auto.setEnabled(false);
            auto.setClickable(false);
        }
    }

    @Override
    public void onAutoSpray(Object o) {
        EnvironmentSpray environmentSpray = (EnvironmentSpray) o;
        if (environmentSpray == null) {
            return;
        }
        environmentSpray.setSprayStatus(1);//喷淋状态:0手动,1自动
        environmentSprayListAdapter.setDataList(environmentSprayList);
        List<EnvironmentSpray> datas = new ArrayList<>();
        datas.add(environmentSpray);
        updateSparyStatus(1, datas);
    }

    @Override
    public void onHandleSpray(Object o) {
        EnvironmentSpray environmentSpray = (EnvironmentSpray) o;
        if (environmentSpray == null) {
            return;
        }
        environmentSpray.setSprayStatus(0);//喷淋状态:0手动,1自动
        environmentSprayListAdapter.setDataList(environmentSprayList);
        List<EnvironmentSpray> datas = new ArrayList<>();
        datas.add(environmentSpray);
        updateSparyStatus(0, datas);
    }

    @Override
    public void onOpenSpray(Object o) {
        EnvironmentSpray environmentSpray = (EnvironmentSpray) o;
        if (environmentSpray == null) {
            return;
        }
//        List<EnvironmentSpray> datas = new ArrayList<>();
//        datas.add(environmentSpray);
        if(Utils.isEmpty(environmentSpray.getStatus()) || environmentSpray.getStatus().equals("0000")){
            startSpary(environmentSpray);
        }else {
            stopSpary(environmentSpray);
        }
    }

    @Override
    public void onThreshold(Object o) {
        EnvironmentSpray environmentSpray = (EnvironmentSpray) o;
        if (environmentSpray == null) {
            return;
        }
        getAlarmSetting(environmentSpray);
    }

    private void initDialog(final List<EnvironmentSpray> datas){
        AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.dialog_no_action_bar);
        View view = View.inflate(this, R.layout.dialog_spray_threshold_settings, null);
        builder.setView(view);
        HXTextView cancel = (HXTextView) view.findViewById(R.id.cancel);
        HXTextView save = (HXTextView) view.findViewById(R.id.save);
        RelativeLayout pm10Reduce = (RelativeLayout) view.findViewById(R.id.pm10_reduce);
        RelativeLayout pm10Plus = (RelativeLayout) view.findViewById(R.id.pm10_plus);
        final HXEditTextView pm10Text = (HXEditTextView) view.findViewById(R.id.pm10_text);
        RelativeLayout pm25Reduce = (RelativeLayout) view.findViewById(R.id.pm25_reduce);
        RelativeLayout pm25Plus = (RelativeLayout) view.findViewById(R.id.pm25_plus);
        final HXEditTextView pm25Text = (HXEditTextView) view.findViewById(R.id.pm25_text);
        RelativeLayout temperatureReduce = (RelativeLayout) view.findViewById(R.id.temperature_reduce);
        RelativeLayout temperaturePlus = (RelativeLayout) view.findViewById(R.id.temperature_plus);
        final HXEditTextView temperatureText = (HXEditTextView) view.findViewById(R.id.temperature_text);
        RelativeLayout humidityReduce = (RelativeLayout) view.findViewById(R.id.humidity_reduce);
        RelativeLayout humidityPlus = (RelativeLayout) view.findViewById(R.id.humidity_plus);
        final HXEditTextView humidityText = (HXEditTextView) view.findViewById(R.id.humidity_text);
        final AlertDialog dialog = builder.create();
        pm10Text.setText(pm10Alarm + "");
        pm25Text.setText(pm25Alarm + "");
        temperatureText.setText(temperatureAlarm + "");
        humidityText.setText(humidityAlarm + "");
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
                saveOrUpdateSetting(datas);
            }
        });
        pm10Plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pm10Alarm++;
                pm10Text.setText(pm10Alarm + "");
            }
        });
        pm10Reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pm10Alarm--;
                pm10Text.setText(pm10Alarm + "");
            }
        });
        pm25Plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pm25Alarm++;
                pm25Text.setText(pm25Alarm + "");
            }
        });
        pm25Reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pm25Alarm--;
                pm25Text.setText(pm25Alarm + "");
            }
        });
        temperaturePlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                temperatureAlarm++;
                temperatureText.setText(temperatureAlarm + "");
            }
        });
        temperatureReduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                temperatureAlarm--;
                temperatureText.setText(temperatureAlarm + "");
            }
        });
        humidityPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                humidityAlarm++;
                humidityText.setText(humidityAlarm + "");
            }
        });
        humidityReduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                humidityAlarm--;
                humidityText.setText(humidityAlarm + "");
            }
        });
        dialog.show();
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.select_dialog_enter);//为弹窗增加入场动画
        view.setAnimation(animation);
        animation.start();
    }

    private void refreshDialog(String title){
        final AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.dialog_no_action_bar);
        View view = View.inflate(this, R.layout.dialog_single_button, null);
        HXTextView titleTv = (HXTextView) view.findViewById(R.id.title_tv);
        HXTextView confirmTv = (HXTextView) view.findViewById(R.id.confirm_tv);
        builder.setView(view);
        titleTv.setText(title);
        final AlertDialog dialog = builder.create();
        confirmTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
                initData();
            }
        });
        dialog.show();
        Window window = dialog.getWindow();
//        window.setBackgroundDrawableResource(R.color.color_transparent);//设置弹窗背景透明，避免白色边角
        WindowManager.LayoutParams params = window.getAttributes();//获取弹窗布局属性
        Display display = getWindowManager().getDefaultDisplay();//获取屏幕参数
        params.width = (int) (display.getWidth()*0.6);//将弹窗宽度设置为屏幕宽度的60%
        window.setAttributes(params);
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.select_dialog_enter);//为弹窗增加入场动画
        view.setAnimation(animation);
        animation.start();
    }

}